module.exports=class extends baseLogic{
    constructor(){
        super();
    }

    static init(){
        return new this();
    }

    async getItems(FindFilterModel,CurPage,PageSize,Sort,curUserInfo){
        let Result={};
        let self=this;
        Result.CurPage = CurPage;
        Result.PageSize = PageSize;
        var FindFilter = {
            IP_IsDel: 0
        };

        if (FindFilterModel.IP_Name) {
            FindFilter.IP_Name = {
                '$like': '%' + FindFilterModel.IP_Name + '%'
            };
        }

       

        if (FindFilterModel.IP_CreateTime) {
            FindFilter.IP_CreateTime = {
                '$between': FindFilterModel.IP_CreateTime.split(',')
            };
        }

       
       
        let data = await this.DB.iframe_platform.Access.findAndCountAll({
            where: FindFilter,
            order: [
                Sort.split('|')
            ],
            offset: (CurPage - 1) * PageSize,
            limit: PageSize,
            raw:true
        });
        let DataList = data.rows;
        DataList.map((item)=>{
            item.IP_CreateTime=this.moment(item.IP_CreateTime).format("YYYY-MM-DD HH:mm:ss");
        });
        Result.RsCount = data.count;
        Result.DataList = DataList;
        return Result;
    }

    async getItem(id){
        let ret  = await this.DB.iframe_platform.Access.findOne({
            where:{
                IP_ID:id
            },
            raw:true
        });
        return ret;
    }

    async insertOrUpdate(body){
        var id = body.IP_ID ? Number(body.IP_ID) : 0;
        var model = {};        
        if (id > 0) {
            model = await this.DB.iframe_platform.Access.findById(id);
            if(!model){
                return false;
            }
            model = model.dataValues;
        } else {
            model = {};
            model.IP_ID=0;
            model.IP_IsDel = 0;
            model.IP_CreateTime=this.moment().format("YYYY-MM-DD HH:mm:ss");
        }
        model.IP_Sign =body.IP_Sign;
        model.IP_Name = body.IP_Name;
        model.IP_Info = body.IP_Info;
    
        if (id > 0) {
            model = await this.DB.iframe_platform.Access.update(model, {
                where: {
                    IP_ID: id
                }
            });
        } else {
            model = await this.DB.iframe_platform.Access.create(model);
        }
       return model;
    }

    async del(keys){
        return await this.DB.iframe_platform.Access.update({ IP_IsDel: 1 }, {
            where: {
                IP_ID: keys
            }
        });
    }
}